//
// Created by yonghu0 on 25-10-7.
//
#include <algorithm> //copy的头文件
#include <list>
#include <iostream>
#include <iterator> //ostream_iterator的头文件

int main()
{
    std::cout << "list_test begin" << std::endl;

    std::cout << "------创建list------" << std::endl;
    std::list<int> l1;
    for (std::list<int>::iterator it = l1.begin(); it != l1.end(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    std::cout << "-------获取size------" << std::endl;
    std::cout << l1.size() << std::endl;
    std::cout << "-------判断list是否为空------" << std::endl;
    std::cout << l1.empty() << std::endl;

    std::cout << "--------------增--------------" << std::endl;
    std::cout << "--头部添加元素--" << std::endl;
    l1.push_front(5);
    l1.push_front(6);
    std::ostream_iterator<int> oit(std::cout, " ");
    std::copy(l1.begin(), l1.end(), oit);
    std::cout << std::endl;
    std::cout << "--尾部添加元素--" << std::endl;
    l1.push_back(3);
    l1.push_back(2);
    l1.push_back(1);
    l1.push_back(4);
    std::copy(l1.begin(), l1.end(), oit);
    std::cout << std::endl;
    std::cout << "--插入元素--" << std::endl;
    // 插入单个元素
    std::list<int>::iterator it1 = std::find(l1.begin(), l1.end(), 2);
    std::list<int>::iterator it2 = l1.insert(it1, 99);
    std::copy(l1.begin(), l1.end(), oit);
    std::cout << std::endl;
    std::cout << *it2 << std::endl;
    // 插入区间
    int a1[] = {11, 12, 13, 14};
    std::list<int>::iterator it3 = l1.insert(it2, a1, a1 + 4);
    std::copy(l1.begin(), l1.end(), oit);
    std::cout << std::endl;
    std::cout << *it3 << std::endl;

    std::cout<<"-------------查找---------------"<<std::endl;
    if (std::find(l1.begin(), l1.end(), 2) != l1.end())
    {
        std::cout<<"容器中存在元素"<<std::endl;
    }
    else
    {
        std::cout<<"容器中不存在元素"<<std::endl;
    }

    std::cout << "-------------删除---------------" << std::endl;
    std::cout << "----头部删除元素-----" << std::endl;
    l1.pop_front();
    std::copy(l1.begin(), l1.end(), oit);
    std::cout << std::endl;
    std::cout << "----尾部删除元素-----" << std::endl;
    l1.pop_back();
    std::copy(l1.begin(), l1.end(), oit);
    std::cout << std::endl;
    std::cout << "----删除元素-----" << std::endl;
    // 删除单个元素
    std::list<int>::iterator it4 = l1.erase(l1.begin());
    std::copy(l1.begin(), l1.end(), oit);
    std::cout << std::endl;
    std::cout << *it4 << std::endl;
    // 删除区间
    std::list<int>::iterator end = l1.begin();
    end++;
    std::list<int>::iterator it5 = l1.erase(l1.begin(), end);
    std::copy(l1.begin(), l1.end(), oit);
    std::cout << std::endl;
    std::cout << *it5 << std::endl;

    std::cout << "-----------------------遍历---------------------" << std::endl;
    std::cout << "---正向遍历---" << std::endl;
    //方法一
    for (auto elem: l1)
    {
        std::cout<<elem<<" ";
    }
    std::cout << std::endl;
    //方法二
    for (std::list<int>::iterator it = l1.begin(); it != l1.end(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    std::cout << "---反向遍历---" << std::endl;
    for (std::list<int>::reverse_iterator it = l1.rbegin(); it != l1.rend(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    std::cout << "-----------清空容器中的所有元素-------------" << std::endl;
    l1.clear();
    std::copy(l1.begin(), l1.end(), oit);
    std::cout << std::endl;

    std::cout << "list_test end" << std::endl;

    return 0;
}
